<template>
    <div class="container-fluid" v-loading="loading">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header"><h4>文章创建</h4></div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8">
                                <div class="form-group">
                                    <label for="summary">会员功能描述</label>
                                    <el-input
                                            type="textarea"
                                            placeholder="会员功能描述"
                                            v-model="form.body"
                                            maxlength="100"
                                            :rows="12"
                                            show-word-limit>
                                    </el-input>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="imageUrl">客服二维码</label>
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/api/upload"
                                            :show-file-list="false"
                                            accept="image/gif, image/jpeg, image/png"
                                            :on-success="handleImgUrlSuccess"
                                            :on-error="handleImgUrlError"
                                            :before-upload="beforeAvatarUpload">

                                        <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <p><button type="submit" class="btn btn-primary" v-on:click="submit()">提交</button></p>
                        </div>

                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "BasePayQrCodeComponent",
        data() {
            return {
                form: {
                    body: '',
                    imageUrl: '',
                },
                loading: false,
                id: $('meta[name="id"]').attr('content')
            }
        },

        methods:{
            handleImgUrlSuccess: function (response) {
                let self = this;
                if (response.code === 200) {
                    self.form.imageUrl = response.url;
                    this.loading = false;
                }
            },
            handleImgUrlError:function(response){
               console.log(response);
            },
            beforeAvatarUpload: function (item) {
                this.loading = true;
            },
            getInfo() {
                let self = this;
                let uri = "setting/pay-qr-code/" + self.id;
                self.loading = true;
                axios.get(uri).then(function (result) {
                    self.loading = false;
                    self.form = result.data;
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            submit: function () {
                let self = this;
                let uri = "setting/pay-qr-code/" + self.id;
                if (!self.form.body) {
                    self.$message.error("必须填写内容");
                    return;
                }
                if (!self.form.imageUrl) {
                    self.$message.error("必须上传客服图");
                    return;
                }
                self.loading = true;
                axios.put(uri, self.form).then(function (result) {
                    self.loading = false;
                    self.$message.success("更新成功！")
                }).catch(function (error) {
                    self.loading = false;
                });
            }
        },
        mounted(){
            this.getInfo();
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .el-upload__input {
        display: none;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }

    .avatar {
        width: 120px;
        height: 120px;
        display: block;
    }
</style>
